      SUBROUTINE regularizaL2(X,iflagF,iflagG)	

      !termo de regularizacao: L2 regularizer

      use globais
      implicit none


      integer :: i,j,ndonoh
      integer::iflagF,iflagG
      double precision ::X(N)
!       double precision, dimension(:,:), allocatable:: L
!       double precision, dimension(:), allocatable:: Y
!       double precision ::DNRM2

      double precision:: aux
      double precision:: deltan2
      ! dá pra fazer sem montar a matriz.... só somando direto o produto.... 
!       allocate(L(N,N))
!       L = 0.d0 ! zera a matriz
      double precision :: dcdxfc, mapXemC
      do i=1,N
	  CX(i) = mapXemC(X(i),cmin,cmax)
	  CXn(i) = mapXemC(Xn(i),cmin,cmax)
      enddo






      if(iflagF.eq.1)then !calcula  Treg


	  if(iflagG.eq.1)then !calcula  grad Treg
	      do i=1,Nx0*Nz0
		  gradTreg(i) = 0.0d0
	      enddo
	  endif
	  Treg = 0.0d0
	  aux = 0.0d0
	  deltan2 = FXn/(deltaX0*deltaZ0) 
! 	  deltan2 = 0.0d0

	  ! calcula Treg com dif finitas progressivas
	  do j=1,Nz0-1
	      do i=1,Nx0-1
		  Ndonoh = (j-1)*Nx0 + i

! 		  Treg = Treg + (CX(Ndonoh+1)-CX(Ndonoh))**2  + (CX(Ndonoh+Nx0)-CX(Ndonoh))**2  + deltan2
! 		  aux  = aux  + (CXn(Ndonoh+1)-CXn(Ndonoh))**2  + (CXn(Ndonoh+Nx0)-CXn(Ndonoh))**2  + deltan2

! 		  Treg = Treg + (X(Ndonoh+1)-X(Ndonoh))**2  + (X(Ndonoh+Nx0)-X(Ndonoh))**2  + deltan2
! 		  aux  = aux  + (Xn(Ndonoh+1)-Xn(Ndonoh))**2  + (Xn(Ndonoh+Nx0)-Xn(Ndonoh))**2  + deltan2

		  Treg = Treg + (X(Ndonoh+1)-X(Ndonoh))**2  + (X(Ndonoh+Nx0)-X(Ndonoh))**2  
		  aux  = aux  + (Xn(Ndonoh+1)-Xn(Ndonoh))**2  + (Xn(Ndonoh+Nx0)-Xn(Ndonoh))**2  

! 		  Treg = Treg +  (X(Ndonoh)-X(Ndonoh+1))**2 + (X(Ndonoh)-X(Ndonoh+Nx0))**2 + &
! 			      &	(X(Ndonoh)-X(Ndonoh-1))**2 + (X(Ndonoh)-X(Ndonoh-Nx0))**2
! 		  aux = aux +  (Xn(Ndonoh)-Xn(Ndonoh+1))**2 + (Xn(Ndonoh)-Xn(Ndonoh+Nx0))**2 + &
! 			      &	(Xn(Ndonoh)-Xn(Ndonoh-1))**2 + (Xn(Ndonoh)-Xn(Ndonoh-Nx0))**2

		  if(iflagG.eq.1)then !calcula  grad Treg
		      gradTreg(Ndonoh+1  ) = gradTreg(Ndonoh+1  ) + 2.0*((X(Ndonoh+1  )-X(Ndonoh)))
		      gradTreg(Ndonoh+Nx0) = gradTreg(Ndonoh+Nx0) + 2.0*((X(Ndonoh+Nx0)-X(Ndonoh)))
		      gradTreg(Ndonoh    ) = gradTreg(Ndonoh    ) - 2.0*((X(Ndonoh+Nx0)-X(Ndonoh))) - 2.0*((X(Ndonoh+1  )-X(Ndonoh)))

! 		      gradTreg(Ndonoh) = gradTreg(Ndonoh) + (2.0*(X(Ndonoh)-X(Ndonoh+1)) + 2.0*(X(Ndonoh)-X(Ndonoh+Nx0)) + &
! 			&	2.0*(X(Ndonoh)-X(Ndonoh-1)) + 2.0*(X(Ndonoh)-X(Ndonoh-Nx0)))
! 		      gradTreg(Ndonoh+1) = gradTreg(Ndonoh+1) - (2.0*(X(Ndonoh)-X(Ndonoh+1)))
! 		      gradTreg(Ndonoh-1) = gradTreg(Ndonoh-1) - (2.0*(X(Ndonoh)-X(Ndonoh-1)))
! 		      gradTreg(Ndonoh+Nx0) = gradTreg(Ndonoh+Nx0) - (2.0*(X(Ndonoh)-X(Ndonoh+Nx0)))
! 		      gradTreg(Ndonoh-Nx0) = gradTreg(Ndonoh-Nx0) - (2.0*(X(Ndonoh)-X(Ndonoh-Nx0)))


		  endif
	      enddo
	  enddo


! 	  ! calcula Treg com dif finitas centrais
! 	  do j=2,Nz0-1   
! 	      do i=2,Nx0-1
! 		  Ndonoh = (j-1)*Nx0 + i
! 		  Treg = Treg + (CX(Ndonoh+1)-CX(Ndonoh-1))**2  + (CX(Ndonoh+Nx0)-CX(Ndonoh-Nx0))**2  + deltan2
! 		  aux  = aux  + (CXn(Ndonoh+1)-CXn(Ndonoh-1))**2  + (CXn(Ndonoh+Nx0)-CXn(Ndonoh-Nx0))**2  + deltan2
! 		  if(iflagG.eq.1)then !calcula  grad Treg
! 		      gradTreg(Ndonoh+1  ) = gradTreg(Ndonoh+1  ) + 2.0d0*(CX(Ndonoh+1  )-CX(Ndonoh-1))
! 		      gradTreg(Ndonoh-1  ) = gradTreg(Ndonoh-1  ) - 2.0d0*(CX(Ndonoh+1  )-CX(Ndonoh-1))
! 		      gradTreg(Ndonoh+Nx0) = gradTreg(Ndonoh+Nx0) + 2.0d0*(CX(Ndonoh+Nx0)-CX(Ndonoh-Nx0))
! 		      gradTreg(Ndonoh-Nx0) = gradTreg(Ndonoh-Nx0) - 2.0d0*(CX(Ndonoh+Nx0)-CX(Ndonoh-Nx0))
! 		  endif
! 	      enddo
! 	  enddo


	  Treg = (Treg + deltan2)/(aux + deltan2)

	  if(iflagG.eq.1)then !calcula  grad Treg
	      do i=1,Nx0*Nz0
! 		  gradTreg(i) = gradTreg(i)*dcdxfc(CX(i),cmin,cmax)/aux
! 		  gradTreg(i) = gradTreg(i)/aux
		  gradTreg(i) = gradTreg(i)/(aux + deltan2)
	      enddo 
	  endif








      endif

	

 


      return
      end subroutine